#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "tower.h"
#include "bullet.h"
#include "texture1.h"
#include "terrain.h"
#include "radar.h"
#include "draw.h"
#include "Vec3.h"
#include "FrustumG.h"
#include "definitions.h"
#include "toolkits/includes/glut.h"
#include "toolkits/includes/glm.h"
#include "toolkits/includes/il.h"

extern GLfloat currentColor[3];
extern GLMmodel *tower;
extern Bullet *bulletClass;
extern Terrain *terrainClass;
extern float towerList[TOWERS][3];
extern float xpos, zpos;
extern time_t timeNow;
int timeBullet;
extern Radar *radarClass;
extern Texture1 *textureClass;
extern Terrain *terrainClass;
extern float altura;
extern float heightMap[TERRAINLENGTH*2][TERRAINLENGTH*2];
extern ILint heightH, heightW;
extern FrustumG frustum;
extern bool frustumOn;

Tower::Tower() {
}

float Tower::calcRotAng(int towerId) {
	float x, z, ang;
	x = xpos - towerList[towerId][0]; // cateto1
	z = zpos - towerList[towerId][2]; // cateto2
	if(z < 0.000) {
		z *= -1;
		ang = atan(x/z); // calcular angulo
		ang = M_PI - ang;
	}
	else {
		ang = atan(x/z); // calcular angulo
	}
	if(z < 0.0) {
		ang = -1 * ang;
	}
	return ang * RAD2ANG; // convert to degrees
}

void Tower::placeTowers() {
	float r = 0.0;
	float alpha;
	float rr;
	float x,z,y;
	float ang;
	float curPos[3] = {xpos, 0.0f, zpos};

	srand(timeNow);
	//srand(74);


	int towers = 0;

	while (towers < TOWERS) {

		rr = rand() * 4050/ RAND_MAX;
		alpha = rand() * 4.74 / RAND_MAX;
		
		x = cos(alpha) * (rr + r);
		z = sin(alpha) * (rr + r);
		//float pos[3] = {x,0,z};

		/*if (fabs(x) < TERRAINLENGTH && fabs(z) < TERRAINLENGTH && !comparePosition(1, towers, pos)) {*/
		if (fabs(x) < TERRAINLENGTH && fabs(z) < TERRAINLENGTH) {
		//if (fabs(x) < heightH && fabs(z) < heightW && x > 0.0f && z > 0.0f) {
			glPushMatrix();
			y = TOWERSCALEFACTOR + heightMap[(int)x+TERRAINLENGTH][(int)z+TERRAINLENGTH];
			insertTower(towers, x, y, z);
			ang = calcRotAng(towers); // calculate rotation angle
			Vec3 a(towerList[towers][0], towerList[towers][1], towerList[towers][2]);
			if (!frustumOn || (frustum.sphereInFrustum(a,TOWERSCALEFACTOR) != FrustumG::OUTSIDE)) {//printf("%d\t-\tSIM\n",towers);
				glPushMatrix();
					glTranslatef(towerList[towers][0], towerList[towers][1], towerList[towers][2]); // translate para meter a torre no sitio
					if(radarClass->distance_calc(curPos, towerList[towers]) <= TOWER_RANGE) { // rodar torre apenas se estiver ao alcance do heroi
						glRotatef(ang, 0.0f, 1.0f, 0.0f); // rodar a torre
					}
					glScalef(TOWERSCALEFACTOR, TOWERSCALEFACTOR, TOWERSCALEFACTOR);
					glmDraw(tower, GLM_MATERIAL | GLM_SMOOTH); // desenha-a
				glPopMatrix();
			}//else printf("NAO\n");
			glPopMatrix();
			towers++;
		}
	}
	glPushMatrix();
		bulletClass->createBullet(curPos);
		bulletClass->drawBullet();
	glPopMatrix();
}

// returns angle in degrees
void Tower::insertTower(int id, float x, float y, float z){
	//towerList[id].id		= id;
	towerList[id][0]	= x;
	towerList[id][1]	= y;
	towerList[id][2]	= z;
	//printf("\nTORRE: %d\nAGORA: %d\n\n", timeNow * 1000, glutGet(GLUT_ELAPSED_TIME));
}